#pragma once

#include "../common/net.hpp"
#include "../common/message.hpp"
#include <future>

/*
    客户端请求接口
    管理请求与响应
*/
namespace rpc
{
    namespace client
    {
        class Requestor
        {
        public:
            using ptr = std::shared_ptr<Requestor>;
            using RequestCallback = std::function<void(const BaseMessage::ptr &)>;
            using AsyncResponse = std::future<BaseMessage::ptr>;

            struct RequestDescribe
            {
                using ptr = std::shared_ptr<RequestDescribe>;
                BaseMessage::ptr request;
                RType rtype;                             // 异步或回调处理response
                std::promise<BaseMessage::ptr> response; // 异步处理
                RequestCallback callback;                // 回调函数
            };

            void onResponse(const BaseConnection::ptr &conn, BaseMessage::ptr &msg)
            {
                RequestDescribe::ptr rdp = getDescribe(msg->rid());
                if (rdp == nullptr)
                {
                    LOG(ERROR, "收到响应 - %s，但是未找到对应的请求描述！", msg->rid().c_str());
                    return;
                }

                if (rdp->rtype == RType::REQ_ASYNC)
                {
                    rdp->response.set_value(msg);
                }
                else if (rdp->rtype == RType::REQ_CALLBACK)
                {
                    if (rdp->callback)
                        rdp->callback(msg);
                }
                else
                {
                    LOG(ERROR, "请求类型未知！！");
                }
                delDescribe(msg->rid());
            }

            bool send(const BaseConnection::ptr &conn, const BaseMessage::ptr &req, AsyncResponse &async_rsp)
            {
                RequestDescribe::ptr rdp = newDescribe(req, RType::REQ_ASYNC);
                if (rdp == nullptr)
                {
                    LOG(ERROR, "构造请求描述对象失败！");
                    return false;
                }
                conn->send(req);
                async_rsp = rdp->response.get_future(); // 异步获取结果
                return true;
            }

            bool send(const BaseConnection::ptr &conn, const BaseMessage::ptr &req, BaseMessage::ptr &rsp)
            {
                AsyncResponse rsp_future;
                bool ret = send(conn, req, rsp_future);
                if (ret == false)
                {
                    return false;
                }
                rsp = rsp_future.get();
                return true;
            }

            bool send(const BaseConnection::ptr &conn, const BaseMessage::ptr &req, const RequestCallback &cb)
            {
                RequestDescribe::ptr rdp = newDescribe(req, RType::REQ_CALLBACK, cb);
                if (rdp.get() == nullptr)
                {
                    LOG(ERROR, "构造请求描述对象失败！");
                    return false;
                }
                conn->send(req);
                return true;
            }

        private:
            // 增删查
            RequestDescribe::ptr newDescribe(const BaseMessage::ptr &msg, RType rtype, const RequestCallback &cb = RequestCallback())
            {
                std::unique_lock<std::mutex> lock(_mutex);
                RequestDescribe::ptr req = std::make_shared<RequestDescribe>();
                req->request = msg;
                req->rtype = rtype;
                if (rtype == RType::REQ_CALLBACK && cb != nullptr)
                {
                    req->callback = cb;
                }
                _request_desc.insert({msg->rid(), req});
                return req;
            }
            RequestDescribe::ptr getDescribe(const std::string &rid)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                auto it = _request_desc.find(rid);
                if (it == _request_desc.end())
                {
                    return RequestDescribe::ptr();
                }
                return it->second;
            }
            void delDescribe(const std::string &rid)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                _request_desc.erase(rid);
            }
            std::mutex _mutex;
            std::unordered_map<std::string, RequestDescribe::ptr> _request_desc; // 请求管理
        };
    }
}